package com.learn.practice.config;

import com.learn.practice.security.*;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

@EnableWebSecurity
public class SpringSecurityConfig {
    @Resource
    private AdminAuthenticationTokenFilter adminAuthenticationTokenFilter;

    @Resource
    private UserDetailServiceImpl userDetailService;
    @Resource
    AuthenticationEntryPointIHandler authenticationEntryPointIHandler;

    @Resource
    AccessDeniedImplHandler accessDeniedImplHandler;

    @Resource
    AdminAuthenticationSuccessHandler adminAuthenticationSuccessHandler;

    @Resource
    AdminAuthenticationFailureHandler adminAuthenticationFailureHandler;

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                //禁用它,因为前后端分离不需要
                .csrf(AbstractHttpConfigurer::disable)
                //禁用session
                .sessionManagement(AbstractHttpConfigurer::disable)
                .authorizeHttpRequests(auhtor-> auhtor
                        //这里配置的接口不需要验证
                        .antMatchers("/hello","/login","/mobile/code","/cache/multiply").permitAll()
                        .antMatchers("/home").hasAuthority("user/admin")
                        //其它接口都需要经过验证
                        .anyRequest().authenticated()

                ).cors();

        AdminUsernamePasswordAuthenticationFilter adminUsernamePasswordAuthenticationFilter = new AdminUsernamePasswordAuthenticationFilter();
        adminUsernamePasswordAuthenticationFilter.setAuthenticationManager(authenticationManager(http));
        //传入我们自定义的成功和失败的处理器
        adminUsernamePasswordAuthenticationFilter.setAuthenticationSuccessHandler(adminAuthenticationSuccessHandler);
        adminUsernamePasswordAuthenticationFilter.setAuthenticationFailureHandler(adminAuthenticationFailureHandler);

        http.addFilterAt(adminUsernamePasswordAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
        http.addFilterBefore(adminAuthenticationTokenFilter,AdminUsernamePasswordAuthenticationFilter.class);

        //注入新的AuthenticationManager
        http.authenticationManager(authenticationManager(http));
        http.exceptionHandling()
                //访问拒绝异常处理器
                .accessDeniedHandler(accessDeniedImplHandler)
                //认证异常处理器
                .authenticationEntryPoint(authenticationEntryPointIHandler);
        return http.build();
    }

    /**
     *密码加密规则
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder(12);
    }

    /**
     *构造一个AuthenticationManager，使用自定义的userDetailsService和passwordEncoder
     */
    @Bean
    AuthenticationManager authenticationManager(HttpSecurity http) throws Exception {
        AuthenticationManager authenticationManager = http.getSharedObject(AuthenticationManagerBuilder.class)
                .userDetailsService(userDetailService)
                .passwordEncoder(passwordEncoder())
                .and()
                .build();
        return authenticationManager;
    }
}
